package com.cyl.h5.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.cyl.manager.pms.service.BrandService;
import com.cyl.manager.ums.domain.entity.UserInfo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.framework.config.LocalDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;


import com.cyl.h5.domain.entity.PmsAppraise;
import com.cyl.h5.domain.convert.PmsAppraiseConvert;
import com.cyl.h5.domain.query.PmsAppraiseQuery;
import com.cyl.h5.domain.vo.PmsAppraiseVO;
import com.cyl.h5.service.PmsAppraiseService;

import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 商品评价表Controller
 *
 * @author zcc
 * @date 2025-05-12
 */
@Api(tags ="商品评价接口列表")
@RestController
@RequestMapping("/app/pmsAppraise")
public class PmsAppraiseController extends BaseController {
    @Autowired
    private PmsAppraiseService service;
    @Autowired
    private PmsAppraiseConvert convert;
    @Autowired
    private BrandService brandService;


    @ApiOperation("查询我的商品评价表列表")
    @PreAuthorize("@ss.hasPermi('h5:pmsAppraise:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<PmsAppraise>> list(@RequestBody PmsAppraiseQuery query, Pageable page) {
        //获取用户id
        UserInfo member = (UserInfo) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        Long id = member.getId();
        query.setUserId(member.getId());
        List<PmsAppraise> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }


    @ApiOperation("好物评价 ")
    @PostMapping("/goodAppraise")
    public ResponseEntity<Page<PmsAppraise>> goodAppraise(@RequestBody PmsAppraiseQuery query, Pageable page) {
        //切割标签id
        if (StringUtils.isNotBlank(query.getLabel())) {
            query.setLabelList(
                    Arrays.stream(query.getLabel().split(","))
                            .map(String::trim)
                            .collect(Collectors.toList())
            );
        }
        List<PmsAppraise> list = service.goodAppraise(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page,list.size()));
    }

    @ApiOperation("好物评价统计 ")
    @GetMapping("/appraiseCount")
    public R<List<Map<String, Object>>> appraiseCount(PmsAppraiseQuery query) {
        return R.ok(service.appraiseCount(query));
    }

    @ApiOperation("导出商品评价表列表")
    @PreAuthorize("@ss.hasPermi('h5:pmsAppraise:export')")
    @Log(title = "商品评价表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(PmsAppraiseQuery query) {
        List<PmsAppraise> list = service.selectList(query, null);
        ExcelUtil<PmsAppraiseVO> util = new ExcelUtil<>(PmsAppraiseVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "商品评价表数据"));
    }

    @ApiOperation("获取商品评价表详细信息")
    @PreAuthorize("@ss.hasPermi('h5:pmsAppraise:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<PmsAppraise> getInfo(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增商品评价表")
    @Log(title = "商品评价表", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody PmsAppraise pmsAppraise) {
        try {
            service.insert(pmsAppraise);
            return R.ok("操作成功");
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("修改商品评价表")
    @PreAuthorize("@ss.hasPermi('h5:pmsAppraise:edit')")
    @Log(title = "商品评价表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody PmsAppraise pmsAppraise) {
        return ResponseEntity.ok(service.update(pmsAppraise));
    }

    @ApiOperation("删除商品评价表")
    @PreAuthorize("@ss.hasPermi('h5:pmsAppraise:remove')")
    @Log(title = "商品评价表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
